package org.Yiran.timetale_re.createtab;

import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.RegistryObject;
import org.Yiran.timetale_re.Timetale_re;
import org.Yiran.timetale_re.item.ItemsRegister;
import org.Yiran.timetale_re.item.MaterialRegister;
import org.Yiran.timetale_re.item.rune.base.RuneItem;
import org.Yiran.timetale_re.item.runeblueprint.base.RuneBlueprintItem;
import org.Yiran.timetale_re.item.tool.screwdriver.CreativeScrewdriverItem;

/**
 * 创造模式物品栏注册类
 * 负责注册和管理模组的创造模式物品栏，包括物品的分类和显示
 */
public class CreativeTabRegister {
    
    // ==================== 注册器定义 ====================
    
    /** 创造模式物品栏注册器 */
    public static final DeferredRegister<CreativeModeTab> CREATIVE_TABS =
            DeferredRegister.create(Registries.CREATIVE_MODE_TAB, Timetale_re.MODID);

    // ==================== 物品栏注册 ====================
    
    /** 时间传说模组创造模式物品栏 */
    public static final RegistryObject<CreativeModeTab> TIMETALE_TAB = CREATIVE_TABS.register("timetale_tab",
            () -> CreativeModeTab.builder()
                    .title(Component.translatable("itemGroup.timetale_re.tab"))
                    // 使用时间宝石作为图标
                    .icon(() -> new ItemStack(ItemsRegister.TIME_GEM.get()))
                    .displayItems((params, output) -> {
                        // 添加除符文外的所有普通物品（仅主标签页物品）
                        ItemsRegister.ModItems.getItemsByTab(ItemsRegister.CreativeTab.MAIN).forEach(item -> {
                            // 排除基础符文物品，因为我们会在下面添加特殊NBT的符文
                            // 排除创造起子，因为会特殊处理
                            if (item != ItemsRegister.ModItems.RUNE && item != ItemsRegister.ModItems.RUNE_BLUEPRINT
                                && item != ItemsRegister.ModItems.CREATIVE_SCREWDRIVER) {
                                output.accept(item.getItem().get());
                            }
                        });

                        // 特殊处理类型物品
                        output.accept(CreativeScrewdriverItem.createCreativeScrewdriver());
                        output.accept(RuneItem.createBlankRune());
                        output.accept(RuneItem.createStormRune());
                        output.accept(RuneItem.createThunderRune());
                        output.accept(RuneItem.createLifeRune());
                        output.accept(RuneBlueprintItem.createStormRuneBlueprint());
                        output.accept(RuneBlueprintItem.createThunderRuneBlueprint());
                        output.accept(RuneBlueprintItem.createLifeRuneBlueprint());

                        // 添加所有金属材料物品
                        addMaterialItemsToTab(output);
                    })
                    .build());
    
    /** 工具类物品创造模式物品栏 */
    public static final RegistryObject<CreativeModeTab> TIMETALE_TOOLS_TAB = CREATIVE_TABS.register("timetale_tools_tab",
            () -> CreativeModeTab.builder()
                    .title(Component.translatable("itemGroup.timetale_re.tools_tab"))
                    .icon(() -> new ItemStack(ItemsRegister.IRON_HAMMER.get()))
                    .displayItems((params, output) -> {
                        // 添加所有工具类物品
                        ItemsRegister.ModItems.getItemsByTab(ItemsRegister.CreativeTab.TOOLS).forEach(item -> {
                            output.accept(item.getItem().get());
                        });
                    })
                    .build());

    // ==================== 材料物品添加方法 ====================
    
    /**
     * 将所有材料物品添加到创造模式物品栏
     * 按照材料类型分类添加物品，确保物品栏组织有序
     * 
     * @param output 创造模式物品栏输出对象
     */
    private static void addMaterialItemsToTab(CreativeModeTab.Output output) {
        // 矿石
        for (MaterialRegister.MaterialDefinition material : MaterialRegister.getRegisteredMaterials()) {
            if (material.hasOre()) {
                // 先注册普通矿石
                if (material.getOreItem() != null) {
                    output.accept(material.getOreItem().get());
                }

                // 再注册深层矿石
                if (material.getDeepslateOreItem() != null) {
                    output.accept(material.getDeepslateOreItem().get());
                }
            }
        }

        // 粗矿
        for (MaterialRegister.MaterialDefinition material : MaterialRegister.getRegisteredMaterials()) {
            if (material.hasOre() && material.getRaw() != null) {
                output.accept(material.getRaw().get());
            }
        }

        // 粗矿块
        for (MaterialRegister.MaterialDefinition material : MaterialRegister.getRegisteredMaterials()) {
            if (material.hasType(MaterialRegister.MaterialType.RAW_BLOCK) && material.hasOre() &&
                    material.getRawBlockItem() != null) {
                output.accept(material.getRawBlockItem().get());
            }
        }

        // 粒
        for (MaterialRegister.MaterialDefinition material : MaterialRegister.getRegisteredMaterials()) {
            if (material.hasType(MaterialRegister.MaterialType.NUGGET) && material.getNugget() != null) {
                output.accept(material.getNugget().get());
            }
        }

        // 锭
        for (MaterialRegister.MaterialDefinition material : MaterialRegister.getRegisteredMaterials()) {
            if (material.hasType(MaterialRegister.MaterialType.INGOT) && material.getIngot() != null) {
                output.accept(material.getIngot().get());
            }
        }

        // 金属块
        for (MaterialRegister.MaterialDefinition material : MaterialRegister.getRegisteredMaterials()) {
            if (material.hasType(MaterialRegister.MaterialType.BLOCK) && material.getBlockItem() != null) {
                output.accept(material.getBlockItem().get());
            }
        }

        // 粉
        for (MaterialRegister.MaterialDefinition material : MaterialRegister.getRegisteredMaterials()) {
            if (material.hasType(MaterialRegister.MaterialType.DUST) && material.getDust() != null) {
                output.accept(material.getDust().get());
            }
        }
    }
}